#include "ColorLegend.h"
#include <GL/glut.h>
#include <string>
#include <iostream>

using namespace std;

//ColorLegend::ColorLegend()
//:
//    d_width(7.3),
//    d_height(1),    
//    d_xpos(0),
//    d_ypos(0),
//    d_numColors(0),
//    d_banded(false)    
//{}

ColorLegend::ColorLegend()
:
    d_width(5.0),
    d_height(1.0),
    d_xpos(0.0),
    d_ypos(0.0),
    d_numColors(0),
    d_banded(false)
{}

ColorLegend::ColorLegend(double width, double height, double x, double y, ColorMap *map)
:
    d_width(width),
    d_height(height),
    d_xpos(x),
    d_ypos(y),
    d_numColors(0),
    d_banded(false),
    d_map(map),
    d_min(0.0),
    d_max(1.0)
{}

ColorLegend::ColorLegend(double width, double height, double x, double y, int colors)
:
    d_width(width),
    d_height(height),
    d_xpos(x),
    d_ypos(y),
    d_numColors(colors),
    d_banded(true)
{}

void ColorLegend::draw(fftw_real wn, fftw_real hn)
{
    drawBackground(wn, hn);    
    drawColorsDisc(wn, hn);    
    drawBorders(wn, hn);
}

void ColorLegend::drawBackground(fftw_real wn, fftw_real hn)
{
    glColor3f(1.0f, 1.0f, 1.0f);
    glBegin(GL_QUADS);
        glVertex2f(wn + d_xpos * wn, hn + (d_ypos + d_height) * hn);              // Top Left
        glVertex2f(wn + d_xpos * wn, hn + d_ypos * hn);              // Bottom Left        
        glVertex2f(wn + (d_xpos + d_width) * wn, hn + d_ypos * hn);              // Bottom Right
        glVertex2f(wn + (d_xpos + d_width) * wn, hn + (d_ypos + d_height) * hn);              // Top Right
    glEnd();    
    
    drawBitmapText("Test", wn + (0.1 * d_width + d_xpos) * wn,
            hn + (0.85 * d_height + d_ypos) * hn);
}
    
void ColorLegend::drawColorsCont(fftw_real wn, fftw_real hn)        
{
    
    fftw_real xpos = d_xpos + 0.1 * d_width;
    fftw_real ypos = d_ypos + 0.1 * d_height;
    fftw_real height = d_height * 0.7;
    fftw_real width = d_width * 0.3;
    
    //cout << "hn: " << hn << "wn: " << wn;
    
    
    d_map->set(0.0);
    glBegin(GL_QUADS);                      // Draw A Quad
    glVertex2f(wn + xpos * wn, hn + ypos * hn);              // Bottom Left       
    glVertex2f(wn + (xpos + width) * wn, hn + ypos * hn);              // Bottom Right
    d_map->set(0.5);
    for (int i = 0; i != 2; ++i)
    {
        glVertex2f(wn + (xpos + width) * wn, hn + (ypos + height / 2) * hn);              // Top Right
        glVertex2f(wn + xpos * wn, hn + (ypos + height / 2) * hn);              // Top Left
    }
    d_map->set(1.0);    
    glVertex2f(wn + xpos * wn, hn + (ypos + height) * hn);              // Top Left
    glVertex2f(wn + (xpos + width) * wn, hn + (ypos + height) * hn);              // Top Right
    
    
    
    
        
        
        
        
        
//        glVertex2f(0.0f, 1.0f);                //Top left
//        glVertex2f(0.0f, -1.0f);               // Bottom Left
//        
//        glColor3f(0.0f, 0.0f, 1.0f);
//        
//        glVertex2f(1.0f, -1.0f);                //Bottom Right
//        glVertex2f(1.0f, 1.0f);                 //Top Right
        
    glEnd();                            // Done Drawing The Quad        
    

    
}

void ColorLegend::drawColorsDisc(fftw_real wn, fftw_real hn)
{
    fftw_real xpos = d_xpos + 0.1 * d_width;
    fftw_real ypos = d_ypos + 0.1 * d_height;
    fftw_real height = d_height * 0.7;
    fftw_real width = d_width * 0.3;
    
    fftw_real min = d_map->usedMin();
    fftw_real max = d_map->usedMax();
    
    int numColors = d_map->levels();
    float dV = (max - min) / (numColors - 1);
    fftw_real dH = height / numColors;
    
    float drawColor = min;
    
    cout << "Draw color: " << drawColor << '\n';
    
    d_map->set(drawColor);
    glBegin(GL_QUADS);
    
    glVertex2f(wn + xpos * wn, hn + ypos * hn); // bottom left
    glVertex2f(wn + (xpos + width) * wn, hn + ypos * hn); // bottom right
    
    for (int block = 1; block != numColors; ++block)
    {
        glVertex2f(wn + (xpos + width) * wn, hn + (ypos + block * dH) * hn); //top right
        glVertex2f(wn + xpos * wn, hn + (ypos + block * dH) * hn); //top right
        drawColor += dV; // next Square
        d_map->set(drawColor);
        glVertex2f(wn + xpos * wn, hn + (ypos + block * dH) * hn); //bottom left
        glVertex2f(wn + (xpos + width) * wn, hn + (ypos + block * dH) * hn); //bottom right
        
    }     
    
    cout << "Draw color: " << drawColor << '\n';
    
    glVertex2f(wn + (xpos + width) * wn, hn + (ypos + height) * hn);    // Top Right
    glVertex2f(wn + xpos * wn, hn + (ypos + height) * hn);              // Top Left
    
    glEnd();   
    
}


void ColorLegend::drawBorders(fftw_real wn, fftw_real hn)
{
    fftw_real xpos = d_xpos + 0.1 * d_width;
    fftw_real ypos = d_ypos + 0.1 * d_height;
    fftw_real height = d_height * 0.7;
    fftw_real width = d_width * 0.3;
    
    glColor3f(0.0f, 0.0f, 0.0f);
    glBegin(GL_LINES);
        glVertex2f(wn + xpos * wn, hn + (ypos + height) * hn);              // Top Left
        glVertex2f(wn + xpos * wn, hn + (ypos) * hn);              // Bottom Left            
        glVertex2f(wn + xpos * wn, hn + (ypos) * hn);              // Bottom Left            
        glVertex2f(wn + (xpos + width) * wn, hn + (ypos) * hn);              // Bottom Right
        glVertex2f(wn + (xpos + width) * wn, hn + (ypos) * hn);              // Bottom Right
        glVertex2f(wn + (xpos + width) * wn, hn + (ypos + height) * hn);              // Top Right
        glVertex2f(wn + (xpos + width) * wn, hn + (ypos + height) * hn);              // Top Right
        glVertex2f(wn + xpos * wn, hn + (ypos + height) * hn);              // Top Left
    glEnd();
}

void drawBitmapText(string const &str, double x, double y)
{    
    glColor3f(0.0f, 0.0f, 0.0f);
    glRasterPos2f(x, y);    
    for (auto itr = str.begin(); itr != str.end(); ++itr)
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *itr);
}

